class singleNode(object):
    def __init__(self,value):
        # value这是存放数据的
        self.value=value
        #存放下一个头结点
        self.next=None  
class SingleLinklist(object):
    def __init__(self,singleNode=None):
        self._head=singleNode
    
    def is_emply(self):
        """判断链表是否为空"""
        return self._head==None

    def length(self):
        cur=self._head
        count=0
        while cur != None:
            count += 1
            cur =cur.next

        return count
    
    def travel(self):
        cur = self._head
        while cur != None:
            print(cur.value,end=" ")    
            cur = cur.next
        print("")

    def add(self,value):
        """头插法，在链表的头部插入数据"""
        node = singleNode(value)
        node.next = self._head                                                                                                            
        self._head = node


    def append(self,value):
        node = singleNode(value)
        if self.is_emply():
            self._head = node
        else:
            cur = self._head
            while cur.next != None:
                cur =cur.next
            cur.next =node
    def insert(self,index,value):
        """这是在链表里插入数值，
        index：插入位置
        value：插入数值"""
        node = singleNode(value)
        if self.is_emply():
            self._head=node
        if index < 0 and index  > self.length:
            self.append(value)
        cur = self._head
        # pre = self._head
        inx = 0
        while inx < index-1:
            inx += 1
            cur = cur.next
        node.next = cur.next
        cur.next = node        
        # pre.next = node
    def remove(self,value):
        cur = self._head
        pre = self._head
        
        while cur.next != None:
            pre = cur 
            cur = cur.next
            if cur.value == value:
                pre.next = cur.next

    def seach(self,value):
        cur = self._head
        while cur.next != None:
            cur = cur.next
            if cur.value == value:
                return True
        return False
        

if __name__== "__main__":
    l = SingleLinklist()

    print(l.is_emply())
    
    l.append(1)
    l.append(2)
    l.append(3)
    l.append(4)
    l.add(5)
    l.add(1)
    l.add(2)
    l.travel()
    l.insert(1,4)
    l.travel()
    print(l.length())
    l.remove(3)
    l.travel()
    print(l.seach(4))